package priv.wangy.micro.common.utils.json.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author zhangjq
 * @since 2024-04-01
 */
public class CstmJacksonDecimalHandler extends JsonSerializer<Number> {

    @Override
    public void serialize(Number number, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (Objects.nonNull(number)) {
            BigDecimal decimal = new BigDecimal(number.toString());
            decimal = decimal.setScale(2, RoundingMode.HALF_UP);
            jsonGenerator.writeNumber(decimal);
        }
    }
}
